package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-12-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    /**
     * 根据父类别ID查询类别树
     * @param id 父类别ID
     * @return 类别树列表
     */
    @Override
    public List<Category> treeSelect(Long id) {
        //根据parentId查找子节点
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, id);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        //如果找到子节点，则为每个类别设置是否有子节点的标志
        if(!CollectionUtils.isEmpty(categoryList)){
            categoryList.forEach(category -> {
                //查找当前节点是否有子节点
                Long count = baseMapper.selectCount(
                        new LambdaQueryWrapper<Category>()
                                .eq(Category::getParentId, category.getId())
                );
                category.setHasChildren(count > 0);
            });
        }
        return categoryList;
    }

    /**
     * 获取所有一级分类
     * @return 一级分类列表
     */
    @Override
    public List<CategoryVo> getLevelOneCategory() {

        //查询所有一级分类
        List<Category> allCategoryList = baseMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0)
        );
        //转换为vo
        return allCategoryList
                //开始流操作
                .stream()
                //中间操作
                .map(category -> {
                    //属性对拷
                    CategoryVo categoryVo = new CategoryVo();
                    BeanUtils.copyProperties(category, categoryVo);
                    return categoryVo;
                })
                //终结操作
                .collect(Collectors.toList());
    }

    /**
     * 获取分类树
     * @return 分类树列表
     */
    @Override
    public List<CategoryVo> tree() {
        //查询所有分类
        List<Category> allCategoryList = baseMapper.selectList(null);
        List<CategoryVo> categoryCollectVoList = allCategoryList
                //开始流操作
                .stream()
                //中间操作
                .map(category -> {
                    CategoryVo categoryVo = new CategoryVo();
                    //属性对拷
                    BeanUtils.copyProperties(category, categoryVo);
                    return categoryVo;
                })
                //终结操作
                .collect(Collectors.toList());
        //构建树形结构
        return buildTree(categoryCollectVoList);
    }

    /**
     * 构建树形结构, 递归
     * @param categoryCollectVoList 分类列表
     * @return 树形结构的分类列表
     */
    private List<CategoryVo> buildTree(List<CategoryVo> categoryCollectVoList) {
        //创建树形结构
        List<CategoryVo> trees = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryCollectVoList) {
            //判断是否是父节点
            if (categoryVo.getParentId() == 0) {
                //查找当前节点的子节点，添加到树形结构中
                trees.add(findChildren(categoryVo, categoryCollectVoList));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param categoryVo 当前节点
     * @param categoryCollectVoList 分类列表
     * @return 带有子节点的当前节点
     */
    private CategoryVo findChildren(CategoryVo categoryVo, List<CategoryVo> categoryCollectVoList) {
        //遍历所有节点，判断是否是当前节点的子节点
        for (CategoryVo it : categoryCollectVoList) {
            if (categoryVo.getId().longValue() == it.getParentId().longValue()) {
                //判断当前节点是否是父节点
                if (categoryVo.getChildren() == null) {
                    categoryVo.setChildren(new ArrayList<CategoryVo>());
                }
                //把子节点放入当前节点的子节点集合中
                categoryVo.getChildren().add(findChildren(it, categoryCollectVoList));
            }
        }
        return categoryVo;
    }
}
